package com.xxx.video.commen.utils;

import android.media.MediaRecorder;
import android.os.Handler;
import android.os.Message;


import java.io.File;

public class RecordVoiceUtils {
    private MediaRecorder mMediaRecorder;
    private String filePath;
    private static RecordVoiceUtils mRecordVoiceUtils = null;
    /**
     *
     */
    private OnRecordListener onRecordListener;
    /**
     * time
     */
    private int time;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (mMediaRecorder != null) {
                /**
                 *
                 */
                time = time + 1000;
                if (onRecordListener != null) {
                    onRecordListener.onRecordProgress(time);
                    mHandler.sendMessageDelayed(new Message(), 1000);
                }
                if (time > 300000) {
                    stopRecord();
                }
            }
        }
    };

    private RecordVoiceUtils() {
    }

    public static RecordVoiceUtils getInstance() {
        if (mRecordVoiceUtils == null) {
            synchronized (RecordVoiceUtils.class) {
                if (mRecordVoiceUtils == null) {
                    mRecordVoiceUtils = new RecordVoiceUtils();
                }
            }
        }
        return mRecordVoiceUtils;
    }

    public void startRecord(String filePath) {
        if (mMediaRecorder == null) {
            mMediaRecorder = new MediaRecorder();
        }
        this.filePath = filePath;
        time = 0;
        try {
            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
            FileUtils.getInstance().checkLocalFilePath(filePath);
            mMediaRecorder.setOutputFile(filePath);
            mMediaRecorder.prepare();
            mMediaRecorder.start();
            mHandler.sendMessageDelayed(new Message(), 100);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setOnRecordListener(OnRecordListener onRecordListener) {
        this.onRecordListener = onRecordListener;
    }

    public interface OnRecordListener {
        /**
         *
         */
        void onRecordProgress(int progress);
    }

    public int getTime() {
        return time;
    }

    public void stopRecord() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }
        if (mMediaRecorder != null) {
            try {
                mMediaRecorder.stop();
                mMediaRecorder.release();
                mMediaRecorder = null;
            } catch (Exception e) {
                mMediaRecorder.reset();
                mMediaRecorder.release();
                mMediaRecorder = null;
                File file = new File(filePath);
                if (file.exists()) {
                    file.delete();
                }
                filePath = "";
            }
        }
    }
}
